package com.android.async;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListView;

import com.android.rest.R;
import com.android.rest.adapter.PostalCodeAdapter;
import com.android.rest.vo.GeoName;
import com.google.android.maps.MapActivity;

public class GeoNamesAsyncTask extends AsyncTask<String, Void, List<GeoName>> {

    private Context ctx = null;

    public GeoNamesAsyncTask(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    protected List<GeoName> doInBackground(String... arg0) {
        return new GeoNamesService().searchPostalCode(arg0[0]);
    }

    @Override
    protected void onPostExecute(List<GeoName> result) {
        this.populateActivity(result);
    }

    void populateActivity(List<GeoName> locations) {

        // Associate Adapter to ListView for matching locations
        MapActivity mapAct = (MapActivity) ctx;
        ListView list = (ListView) mapAct.findViewById(R.id.list);

        PostalCodeAdapter adapter = new PostalCodeAdapter(this.ctx, locations);
        list.setAdapter(adapter);

    }
}